{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## while循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-21T23:51:47.441472Z",
     "start_time": "2023-07-21T23:51:47.437236Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "\n",
    "while count <= 9:\n",
    "    print(count)\n",
    "    count += 1  # count = count + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "不想每打印一个数 就打印一次回车："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 2 3 4 5 6 7 8 9 \n"
     ]
    }
   ],
   "source": [
    "# 字符串拼接法\n",
    "result = ''\n",
    "count = 0\n",
    "\n",
    "while count <= 9:\n",
    "    result += (str(count) + ' ')\n",
    "    count += 1  # count = count + 1\n",
    "    \n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 2 3 4 5 6 7 8 9 end\n"
     ]
    }
   ],
   "source": [
    "# 令 end=' '\n",
    "\n",
    "count = 0\n",
    "\n",
    "while count <= 9:\n",
    "    print(count, end=' ') # 参数end: 打印完内容后，再打印 end内容，默认‘\\n’\n",
    "    count += 1\n",
    "\n",
    "print('end')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-21T23:53:52.356563Z",
     "start_time": "2023-07-21T23:53:52.352684Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 2 3 4 5 6 7 8 9 \n",
      "Done\n"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "\n",
    "while count <= 9:\n",
    "    print(count, end=' ') \n",
    "    count += 1  # count = count + 1\n",
    "    \n",
    "print() # 换行\n",
    "print('Done')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## while...else语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-21T23:58:48.097742Z",
     "start_time": "2023-07-21T23:58:48.093531Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 2 3 4 5 6 7 8 9 end\n"
     ]
    }
   ],
   "source": [
    "# while语句执行完之后，自动执行 else里的内容\n",
    "\n",
    "count = 0\n",
    "\n",
    "while count <= 9:\n",
    "    print(count, end=' ')\n",
    "    count += 1\n",
    "\n",
    "else:\n",
    "    print('end')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## for循环\n",
    "每次循环，会从集合中取得一个元素，并执行一次代码块，直到集合中所有的元素都获取\n",
    "\n",
    "可以遍历的对象有：序列(字符串、列表、元组) 、字典、迭代器对象(iterator) 、生成器函数文件对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T00:04:01.067009Z",
     "start_time": "2023-07-22T00:04:01.061827Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tom\n",
      "Peter\n",
      "Jerry\n",
      "Jack\n"
     ]
    }
   ],
   "source": [
    "# 列表 -- 可迭代对象\n",
    "names = ['Tom', 'Peter', 'Jerry', 'Jack']\n",
    "\n",
    "# 遍历列表\n",
    "for name in names:\n",
    "    print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T00:06:44.788587Z",
     "start_time": "2023-07-22T00:06:44.777995Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# range()：构造range对象，可迭代\n",
    "# list()：强转为列表类型\n",
    "\n",
    "list(range(4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T00:07:39.214456Z",
     "start_time": "2023-07-22T00:07:39.211134Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 Tom\n",
      "1 Peter\n",
      "2 Jerry\n",
      "3 Jack\n"
     ]
    }
   ],
   "source": [
    "for i in range(len(names)): # len(names)为 4\n",
    "    \n",
    "    print(i, names[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 循环嵌套"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### while嵌套"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T00:16:57.035071Z",
     "start_time": "2023-07-22T00:16:57.029723Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1\t\n",
      "2*1=2\t2*2=4\t\n",
      "3*1=3\t3*2=6\t3*3=9\t\n",
      "4*1=4\t4*2=8\t4*3=12\t4*4=16\t\n",
      "5*1=5\t5*2=10\t5*3=15\t5*4=20\t5*5=25\t\n",
      "6*1=6\t6*2=12\t6*3=18\t6*4=24\t6*5=30\t6*6=36\t\n",
      "7*1=7\t7*2=14\t7*3=21\t7*4=28\t7*5=35\t7*6=42\t7*7=49\t\n",
      "8*1=8\t8*2=16\t8*3=24\t8*4=32\t8*5=40\t8*6=48\t8*7=56\t8*8=64\t\n",
      "9*1=9\t9*2=18\t9*3=27\t9*4=36\t9*5=45\t9*6=54\t9*7=63\t9*8=72\t9*9=81\t\n"
     ]
    }
   ],
   "source": [
    "# 打印九九乘法表\n",
    "j = 1\n",
    "while j <= 9:\n",
    "    i = 1\n",
    "    \n",
    "    while i <= j:\n",
    "        \n",
    "        # 格式化输出\n",
    "        print(\"%d*%d=%d\"%(j, i, i*j), end='\\t')\n",
    "        i += 1\n",
    "        \n",
    "    print()\n",
    "    j += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### for嵌套"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 5, 7, 9]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成可迭代对象，step为2\n",
    "list(range(1, 10, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T00:20:30.266582Z",
     "start_time": "2023-07-22T00:20:30.260596Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1\t\n",
      "2*1=2\t2*2=4\t\n",
      "3*1=3\t3*2=6\t3*3=9\t\n",
      "4*1=4\t4*2=8\t4*3=12\t4*4=16\t\n",
      "5*1=5\t5*2=10\t5*3=15\t5*4=20\t5*5=25\t\n",
      "6*1=6\t6*2=12\t6*3=18\t6*4=24\t6*5=30\t6*6=36\t\n",
      "7*1=7\t7*2=14\t7*3=21\t7*4=28\t7*5=35\t7*6=42\t7*7=49\t\n",
      "8*1=8\t8*2=16\t8*3=24\t8*4=32\t8*5=40\t8*6=48\t8*7=56\t8*8=64\t\n",
      "9*1=9\t9*2=18\t9*3=27\t9*4=36\t9*5=45\t9*6=54\t9*7=63\t9*8=72\t9*9=81\t\n"
     ]
    }
   ],
   "source": [
    "# 左闭右开 [1, 10)，step默认为1\n",
    "for j in range(1, 10):\n",
    "    \n",
    "    for i in range(1, j+1):\n",
    "        print(\"%d*%d=%d\"%(j, i, i*j), end='\\t')\n",
    "       \n",
    "    print()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "288px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
